package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.oa.entity.ReimburseInvoice;
import com.mxpio.erp.common.oa.entity.ReimburseInvoiceLine;
import com.mxpio.erp.common.oa.service.ReimburseInvoiceLineService;
import com.mxpio.erp.common.oa.service.ReimburseInvoiceService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ReimburseInvoiceController", description = "财务报销-发票")
@RestController
@RequestMapping("/erp/oa/reimburse/invoice/")
public class ReimburseInvoiceController {

    @Autowired
    private ReimburseInvoiceService reimburseInvoiceService;
    @Autowired
    private ReimburseInvoiceLineService reimburseInvoiceLineService;


    @GetMapping("list")
    @Operation(summary = "发票单列表", description = "发票单列表（无分页）", method = "GET")
    public Result<List<ReimburseInvoice>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ReimburseInvoice> list = reimburseInvoiceService.list(ReimburseInvoice.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "发票单列表", description = "发票单列表（分页）", method = "GET")
    public Result<Page<ReimburseInvoice>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ReimburseInvoice> pageResult = reimburseInvoiceService.listPage(ReimburseInvoice.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取发票单", description = "根据ID获取发票单", method = "GET")
    public Result<ReimburseInvoice> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        ReimburseInvoice reimburseInvoice = reimburseInvoiceService.getById(ReimburseInvoice.class, bizNo);
        return Result.OK(reimburseInvoice);
    }

    /**
     * 保存or编辑接口
     * @param reimburseInvoice
     * @return
     */
    @RequestMapping("save")
    @Operation(summary = "保存发票表", description = "保存发票表")
    public Result<ReimburseInvoice> add(@RequestBody ReimburseInvoice reimburseInvoice) {
//        if (reimburseInvoice.getCrudType().equals(CrudType.UPDATE)){
//            List<ReimburseReconciliation> list = reimburseReconciliationService.listByBizNo(reimburseInvoice.getBizNo());
//            if (!list.isEmpty()){
//                return Result.error("发票已勾稽，不能修改");
//            }
//        }
        reimburseInvoiceService.save(reimburseInvoice, new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context){
                Object o = context.getEntity();
                if (o instanceof ReimburseInvoiceLine){
                    ReimburseInvoice parent = context.getParent();
                    ((ReimburseInvoiceLine) o).setBizNo(parent.getBizNo());
                }
                return true;
            }
        });
        return Result.OK(reimburseInvoice);
    }

//    @PutMapping("edit")
//    @Operation(summary = "编辑", description = "编辑", method = "PUT")
//    public Result<ReimburseInvoice> edit(@RequestBody ReimburseInvoice reimburseInvoice) {
//        ReimburseInvoice byId = reimburseInvoiceService.getById(ReimburseInvoice.class,reimburseInvoice.getBizNo());
//        if (!byId.getOrderStatus().equals(OaEnums.OrderStatus.NEW.getCode())){
//            return Result.error("在新增状态下才可修改");
//        }
//        reimburseInvoiceService.update(reimburseInvoice);
//        return Result.OK(reimburseInvoice);
//    }

    @DeleteMapping("remove/{bizNos}")
    @Operation(summary = "根据bizNo删除销售发票", description = "删除销售发票", method = "DELETE")
    public Result<ReimburseInvoice> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "bizNos", required = true) String bizNos) {
        String[] bizNoP = bizNos.split(",");
//        for (String bizNo : bizNoP){
//            List<ReimburseReconciliation> list = reimburseReconciliationService.listByBizNo(bizNo);
//            if (!list.isEmpty()){
//                return Result.error("发票已勾稽，不能删除");
//            }
//        }
        for(String bizNo : bizNoP){
            reimburseInvoiceService.delete(ReimburseInvoice.class, bizNo);
        }
        return Result.OK();
    }

//    @GetMapping("list/reimburse")
//    @Operation(summary = "查询未勾稽的报销单", description = "查询未勾稽的报销单（无分页）", method = "GET")
//    public Result<List<ReimburseInvoice>> listReimburse(@Parameter(description="查询参数") Criteria criteria) {
//        Criteria criteria = Criteria.create().addCriterion("oaBizNo", Operator.EQ, oaBizNo);
//        List<ReimburseInvoice> list = reimburseInvoiceService.list(ReimburseInvoice.class,criteria);
//        return Result.OK(list);
//    }



    /**
     * 通过报销单编码查询数据
     * @param
     * @return
     */
    @GetMapping("list/{oaBizNo}")
    @Operation(summary = "通过报销单编码查询勾稽的报销单", description = "通过报销单编码查询勾稽的报销单（无分页）", method = "GET")
    public Result<List<ReimburseInvoice>> listReim(@PathVariable(name = "oaBizNo", required = true) String oaBizNo) {
        Criteria criteria = Criteria.create().addCriterion("oaBizNo", Operator.EQ, oaBizNo);
        List<ReimburseInvoice> list = reimburseInvoiceService.list(ReimburseInvoice.class,criteria);
        return Result.OK(list);
    }

    @RequestMapping("save/oa")
    @Operation(summary = "勾稽报销单", description = "勾稽报销单")
    public Result<ReimburseInvoice> hook(@RequestBody List<ReimburseInvoice> reimburseInvoice) {
        for (ReimburseInvoice invoice : reimburseInvoice) {
            ReimburseInvoice byId = reimburseInvoiceService.getById(ReimburseInvoice.class, invoice.getBizNo());
            byId.setOaBizNo(invoice.getOaBizNo());
            reimburseInvoiceService.update(byId);
        }
        return Result.OK();
    }

}
